package com.example.administrator.zhongyichou.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.zhongyichou.Bean.BaseEventBean;
import com.example.administrator.zhongyichou.base.BaseActivity;
import com.example.administrator.zhongyichou.config.Constants;
import com.example.administrator.zhongyichou.fragment.HomeFragment;
import com.example.administrator.zhongyichou.fragment.MutualAidFragment;
import com.example.administrator.zhongyichou.fragment.MyFragment;
import com.example.administrator.zhongyichou.fragment.NewsFragment;
import com.example.administrator.zhongyichou.net.core.HttpLoader;
import com.example.administrator.zhongyichou.net.protocol.GsonBaseProtocol;
import com.example.administrator.zhongyichou.utils.ToastUtil;
import com.example.administrator.zhongyichou.view.HeaderView;
import com.example.administrator.zhongyichou.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTouch;

public class MainActivity extends BaseActivity {


    @BindView(R.id.fl_content)
    FrameLayout flContent;
    @BindView(R.id.headerview)
    HeaderView headerview;
    @BindView(R.id.ll_bottom1)
    LinearLayout llBottom1;
    @BindView(R.id.ll_bottom2)
    LinearLayout llBottom2;
    @BindView(R.id.ll_bottom3)
    LinearLayout llBottom3;
    @BindView(R.id.ll_bottom4)
    LinearLayout llBottom4;
    @BindView(R.id.ll_bottom5)
    LinearLayout llBottom5;
    @BindView(R.id.img1)
    ImageView img1;
    @BindView(R.id.img2)
    ImageView img2;
    @BindView(R.id.img3)
    ImageView img3;
    @BindView(R.id.img4)
    ImageView img4;
    @BindView(R.id.txt1)
    TextView txt1;
    @BindView(R.id.txt2)
    TextView txt2;
    @BindView(R.id.txt3)
    TextView txt3;
    @BindView(R.id.txt4)
    TextView txt4;
    private FragmentTransaction fragmentTransaction;
    public FragmentManager fragmentManager;
    private ArrayList<Fragment> fragments;

    @Override
    protected void init() {
        ButterKnife.bind(this);
        initHead();
        initFragment();
    }

    //初始化fragment
    private void initFragment() {
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragments = new ArrayList<>();
        HomeFragment homeFragment = new HomeFragment();
        MutualAidFragment mutualAidFragment = new MutualAidFragment();
        NewsFragment newsFragment = new NewsFragment();
        MyFragment myFragment = new MyFragment();
        fragments.add(homeFragment);
        fragmentTransaction.add(R.id.fl_content, homeFragment);
        fragments.add(mutualAidFragment);
        fragmentTransaction.add(R.id.fl_content, mutualAidFragment);
        fragments.add(newsFragment);
        fragmentTransaction.add(R.id.fl_content, newsFragment);
        fragments.add(myFragment);
        fragmentTransaction.add(R.id.fl_content, myFragment);
        fragmentTransaction.commit();
        selectBtn(0);
    }

    //初始化头部
    private void initHead() {
        headerview.setLeftIconVisibility(View.GONE);
    }

    @Override
    protected void responseData(GsonBaseProtocol mGsonBaseProtocal) {
    }

    @Override
    protected HttpLoader initHttpLoader() {
        return null;
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_main;
    }

    @Override
    public boolean onEventBus(BaseEventBean baseEventBean) {

        return false;
    }

    @OnClick({R.id.ll_bottom1, R.id.ll_bottom2, R.id.ll_bottom3, R.id.ll_bottom4, R.id.ll_bottom5})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_bottom1:
                clearBtn();
                selectBtn(0);
                break;
            case R.id.ll_bottom2:
                clearBtn();
                selectBtn(1);
                break;
            case R.id.ll_bottom3://发起筹款
                Intent intent = new Intent(this, FundraisingActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.slide_bottom_in, 0);
                break;
            case R.id.ll_bottom4:
                clearBtn();
                selectBtn(2);
                break;
            case R.id.ll_bottom5:
                clearBtn();
                selectBtn(3);
                break;
        }
    }

    @OnTouch({R.id.ll_bottom1, R.id.ll_bottom2, R.id.ll_bottom3, R.id.ll_bottom4, R.id.ll_bottom5})
    public boolean onTouch(View v, MotionEvent event) {
        //设置Animation
        Animation animDwon = AnimationUtils.loadAnimation(this, R.anim.show_down);
        Animation animUp = AnimationUtils.loadAnimation(this, R.anim.show_up);
        LinearLayout ll = (LinearLayout) v;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                ll.startAnimation(animDwon);
                animDwon.setFillAfter(true);
                break;
            case MotionEvent.ACTION_UP:
                ll.startAnimation(animUp);
                animUp.setFillAfter(true);
                break;
        }
        return false;
    }

    //选中
    private void selectBtn(int position) {
        switchFragment(position);
        switch (position) {
            case 0:
                img1.setImageResource(R.mipmap.tab_home_p);
                txt1.setTextColor(getResources().getColor(R.color.appColor));
                headerview.setTitle("首页");
                break;
            case 1:
                img2.setImageResource(R.mipmap.tab_publicwelfare_p);
                txt2.setTextColor(getResources().getColor(R.color.appColor));
                headerview.setTitle("公益互助");
                break;
            case 2:
                img3.setImageResource(R.mipmap.tab_news_p);
                txt3.setTextColor(getResources().getColor(R.color.appColor));
                headerview.setTitle("消息");
                break;
            case 3:
                img4.setImageResource(R.mipmap.tab_me_p);
                txt4.setTextColor(getResources().getColor(R.color.appColor));
                headerview.setTitle("我的");
                break;
        }
    }

    private void clearBtn() {
        img1.setImageResource(R.mipmap.tab_home_n);
        img2.setImageResource(R.mipmap.tab_publicwelfare_n);
        img3.setImageResource(R.mipmap.tab_news_n);
        img4.setImageResource(R.mipmap.tab_me_n);
        txt1.setTextColor(getResources().getColor(R.color.black_text9));
        txt2.setTextColor(getResources().getColor(R.color.black_text9));
        txt3.setTextColor(getResources().getColor(R.color.black_text9));
        txt4.setTextColor(getResources().getColor(R.color.black_text9));
    }

    /**
     * 选择隐藏与显示的Fragment
     *
     * @param index 显示的Frgament的角标
     */
    private void switchFragment(int index) {
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        for (int i = 0; i < fragments.size(); i++) {
            if (index == i) {
                fragmentTransaction.show(fragments.get(index));
            } else {
                fragmentTransaction.hide(fragments.get(i));
            }
        }
        fragmentTransaction.commit();
    }


    private long exitTime = 0l;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {

            if ((System.currentTimeMillis() - exitTime) > 2000) {
                ToastUtil.showToast("再次点击退出程序");
                exitTime = System.currentTimeMillis();
            } else {
                finishBase();
            }

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
